package com.alienware.carlauncherlite.Dock;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.GridView;

import androidx.annotation.NonNull;

import com.alienware.carlauncherlite.R;

import java.util.ArrayList;
import java.util.List;

public class DockDialog extends Dialog {
    private final Context context;
    private static GridView allAppsGridView = null;
    private List<DockItemInfo> allItemInfoList = null;

    public DockDialog(@NonNull Context context, List<DockItemInfo> allItemInfoList) {
        super(context, R.style.DockDialog);
        this.context = context;
        this.allItemInfoList = allItemInfoList;
    }

    public GridView getAllAppsGridView() {
        return allAppsGridView;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //对话框布局
        this.setContentView(R.layout.gridview_layout);
//        getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
        allAppsGridView = (GridView) findViewById(R.id.dialogGridViewApps);
        //获取数据
        allItemInfoList = DockItemManager.GetAllAppList(context);
        DockDialogAdapter dockDialogAdapter = new DockDialogAdapter(allItemInfoList, context);
        allAppsGridView.setAdapter(dockDialogAdapter);
        //监听器
        allAppsGridView.setOnItemClickListener((adapterView, view, i, l) -> {
            DockDialog.this.dismiss();
            DockItemInfo dockItemInfo = allItemInfoList.get(i);
            String packageName = dockItemInfo.getPackageName();
            if (packageName.equals(DockItemManager.sponsorItemName)) {
                SponsorDialog sponsorDialog = new SponsorDialog(context);
                sponsorDialog.show();
            } else {
                Intent intent = context.getPackageManager().getLaunchIntentForPackage(allItemInfoList.get(i).getPackageName());
                if (intent != null) {
                    context.startActivity(intent);
                }
            }
        });
    }
}